<?php

namespace app\cms\controller\v1;

use app\common\service\Express as ExpressService;
use app\cms\validate\Express as ExpressValidate;
use app\common\exception\Client;
use app\common\validate\PagingParameter;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\response\Json;

class Express
{
    /**
     * cms端快递入库
     * @param Request $request
     * @return Json
     * @throws Client
     * @throws DbException
     */
    public function create(Request $request): Json
    {
        // 1. 参数接收
        $params = $request->only(['tracking_number']);
        // 2. 验证层校验
        $validate = new ExpressValidate();
        if(!$validate->scene('create')->check($params)){
            throw new Client(212001,$validate->getError());
        }
        // 3. 调用服务层
        return ExpressService::addExpress($params['tracking_number']);
    }

    /**
     * cms端更新快递状态
     * @param Request $request
     * @return Json
     * @throws Client
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function update(Request $request): Json{
        // 1. 参数接收
        $params = $request->only(['id','status']);
        // 2. 验证层校验
        $validate = new ExpressValidate();
        if(!$validate->scene('update')->check($params)){
            throw new Client(212001,$validate->getError());
        }
        // 3. 调用服务层
        ExpressService::updateStatus($params);
        return writeJson(200,$params,'修改状态成功');
    }

    /**
     * CMS端分页获取快递列表
     * @param Request $request
     * @return Json
     * @throws Client
     */
    public function list(Request $request): Json
    {
        // 1. 接收参数
        $params = $request->only(['page','pagesize']);
        // 2. 参数验证
        $validate = new PagingParameter();
        if(!$validate->check($params))throw new Client(223001,$validate->getError());
        // 3. 调用服务层
        $res = (new ExpressService())->list($params);
        // 4. 返回结果
        return writeJson(200,$res,'查询成功');
    }



}